package com.yqg.protection.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.protection.common.Result;
import com.yqg.protection.controller.dto.SetParamArray;
import com.yqg.protection.controller.dto.StatusArray;
import com.yqg.protection.controller.dto.TimeswitchArray;
import com.yqg.protection.entity.Equipment;
import com.yqg.protection.entity.EquipmentInputStore;
import com.yqg.protection.entity.SimInputStore;
import com.yqg.protection.service.IEquipmentInputStoreService;
import com.yqg.protection.service.IEquipmentService;
import com.yqg.protection.service.ISimInputStoreService;
import com.yqg.protection.utils.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * EquipmentController 前端控制器
 * @author zxf
 * @since 2024-01-01
 */
@RestController
@Api(tags = "设备控制器")
@RequestMapping("/equipment")
public class EquipmentController {

    @Resource
    private IEquipmentService equipmentService;

    @Resource
    private IEquipmentInputStoreService equipmentInputStoreService;

    @Resource
    private ISimInputStoreService simInputStoreService;

    /**
    *  新增或者更新
    */
    @ApiOperation("新增或者更新")
    @PostMapping("/edit")
    public Result save(@RequestBody Equipment equipment) {
        if (ObjectUtil.isEmpty(equipment.getEntererId())){
            equipment.getEnterDate();
        }
        equipment.setIsDelete(0);

        return Result.success(equipmentService.saveOrUpdate(equipment));
    }
    /**
    *  删除
    */
    @ApiOperation("删除")
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Equipment equipment =equipmentService.getById(id);
        equipment.setIsDelete(1);
        return Result.success(equipmentService.saveOrUpdate(equipment));
    }
    /**
    *  查询所有
    */
    @ApiOperation("查询所有")
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
       List<Equipment> list=  equipmentService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(equipmentService.getById(id));
    }
    /**
    *  分页查询
    */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        return Result.success(equipmentService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("更新组织")
    @PostMapping("/updateOrganization")
    public Result updateOrganization(@RequestBody Equipment equipment) {
        if (equipment.getState().equals("0")) {
            return Result.error(Constants.CODE_211, "设备在线，无法解除绑定");
        }
        LambdaQueryWrapper<EquipmentInputStore> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentInputStore::getEquipmentId,equipment.getEquipmentId());
        EquipmentInputStore equipmentInputStore = equipmentInputStoreService.getOne(queryWrapper);
        if (equipmentInputStore == null) {
            return Result.error(Constants.CODE_211, "设备不存在");
        }
        if (equipmentInputStore.getState().equals("1")) {
            return Result.error(Constants.CODE_211, "设备已注销");
        }
        equipmentInputStore.setOrganization(equipment.getOrganization());
        equipmentInputStoreService.saveOrUpdate(equipmentInputStore);
        LambdaQueryWrapper<SimInputStore> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SimInputStore::getSimId,equipment.getSimId());
        SimInputStore simInputStore = simInputStoreService.getOne(queryWrapper1);
        if (simInputStore == null) {
            return Result.error(Constants.CODE_211, "sim卡不存在");
        }
        if (simInputStore.getState().equals("1")) {
            return Result.error(Constants.CODE_211, "sim卡已注销");
        }
        simInputStore.setOrganization(equipment.getOrganization());
        simInputStoreService.saveOrUpdate(simInputStore);
        equipmentService.saveOrUpdate(equipment);
        return Result.success();
    }

    /**
     *  设置网关参数(CMD-3)
     */
    @ApiOperation("设置网关参数(CMD-3)")
    @GetMapping("/setHeartbeat")
    public void setHeartbeat(@RequestParam String deviceId, @RequestParam Integer heartbeat) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-3");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        HashMap<String, Object> desired = new HashMap<>();
        desired.put("heartbeat",heartbeat);
        map.put("desired",desired);
    }

    /**
     * 设置基本参数(CMD-4)
     */
    @ApiOperation("设置基本参数(CMD-4)")
    @GetMapping("/setBasicParameter")
    public void setBasicParameter(@RequestParam String deviceId, @RequestParam List<SetParamArray> arrays) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-4");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        HashMap<String, Object> desired = new HashMap<>();
        desired.put("arrays",arrays);
        map.put("desired",desired);
    }

    /**
     * 设置开关定时参数(CMD-5)
     */
    @ApiOperation("设置开关定时参数(CMD-5)")
    @GetMapping("/setSwitchTimeParam")
    public void setSwitchTimeParam(@RequestParam String deviceId, @RequestParam List<TimeswitchArray> arrays) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-5");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        HashMap<String, Object> desired = new HashMap<>();
        desired.put("arrays",arrays);
        map.put("desired",desired);
    }

    /**
     * 设备开关控制指令(CMD-6)
     */
    @ApiOperation("设备开关控制指令(CMD-6)")
    @GetMapping("/setSwitchControl")
    public void setSwitchControl(@RequestParam String deviceId, @RequestParam List<StatusArray> arrays) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-6");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        HashMap<String, Object> desired = new HashMap<>();
        desired.put("arrays",arrays);
        map.put("desired",desired);
    }

    /**
     *  漏电检测指令(CMD-7)
     */
    @ApiOperation("漏电检测指令(CMD-7)")
    @GetMapping("/setLeakageDetection")
    public void setLeakageDetection(@RequestParam String deviceId, @RequestParam List<StatusArray> arrays) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-7");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        HashMap<String, Object> desired = new HashMap<>();
        desired.put("arrays",arrays);
        map.put("desired",desired);
    }

    /**
     *  恢复出厂设置(CMD-8)
     */
    @ApiOperation("恢复出厂设置(CMD-8)")
    @GetMapping("/setReset")
    public void setReset(@RequestParam String deviceId, @RequestParam Integer reset) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-8");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        HashMap<String, Object> desired = new HashMap<>();
        desired.put("reset",reset);
        map.put("desired",desired);
    }

    /**
     *  控制外部 485 设备(CMD-50)
     */
    @ApiOperation("控制外部 485 设备(CMD-50)")
    @GetMapping("/conExter485Dev")
    public void conExter485Dev(@RequestParam String deviceId, @RequestParam List<StatusArray> arrays) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-50");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        HashMap<String, Object> desired = new HashMap<>();
        desired.put("arrays",arrays);
        map.put("desired",desired);
    }

    /**
     *  设置远程分闸锁定模式(CMD-11)
     */
    @ApiOperation("设置远程分闸锁定模式(CMD-11)")
    @GetMapping("/setSwitchMode")
    public void setSwitchMode(@RequestParam String deviceId, @RequestParam List<StatusArray> arrays) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("messageid",new Date().getTime());
        map.put("timestamp",new Date().getTime()/1000);
        map.put("deviceid",deviceId);
        map.put("cmd","CMD-11");
//        map.put("signal",10);
//        map.put("sign","aaaaa");
        map.put("desired",arrays);
    }
}

